import BaseChannel from "./BaseChannel";
// import zsSdk = require("../../lib/zs.sdk.js");
import { compareVersion, isNull } from "../SDKConfig";
import ZSGameItemModel from "./ZSGameItemModel";
import GameItemModel from "./GameItemModel";
import { EventName } from "../../public/Config";
import GlobalEvent from "../../cfw/event/GlobalEvent";
import User from "../../game/db/User";
import TSMap from "../../cfw/struct/TSMap";
import RandomHelper from "../../cfw/tools/RandomHelper";
let osMap = {
    'Android': 'android',
    "iOS": 'ios'
}
export default class ZSChannel extends BaseChannel {

    protected openID: string = ''
    protected verson: string = '1.0';
    protected adData: ZSGameItemModel[] = [];

    protected clickMap: TSMap<string, GameItemModel[]> = new TSMap()
    protected zsConfig = {
        zs_version: '1.0',
        zs_switch: 0,//误触总开关(1-开 0-关)
        zs_banner_city: 0,
        zs_advert_change: -1,//狂点界面banner、多格子广告切换开关（返回-1只弹banner，返回1只弹多格子广告，返回0轮换
        zs_auto_jump_switch2: 0,//
        zs_click_award_num2: 1,//上厕所狂点开关（1开0关）
        zs_random_jump: 0,//自动跳转开关（1开0关
        zs_native_click_switch: 1,
        zs_loading_video: 0,//加载进入游戏自动弹视频开关（1开0关）
        zs_banner_force_click: 1,//返回1时按目前的逻辑，返回0时banner只展示1次
        zs_click_award_video: 0,//狂点界面自动弹视频开关
        zs_video_delayed: 0,//视频延时套路开关（1开0关）
        zs_auto_jump_switch: 0,//增加强跳转功能：进入界面后先自动随机弹出一次跳转
        zs_banner_time: 2,//全屏导出页banner展示时长（返回多少就展示几秒）
        zs_begin_click_switch: 0,//开局砸金蛋开关(1开 0关）
        zs_start_video_switch: 0,//开局看视频开关（1开 0关）
        zs_begin_click__since: 3,//开局砸金蛋用户第几次进入游戏时出现
        zs_banner_vertical_enable: 0,//banner文字误触文字上移开关（0关，1开）
        zs_banner_system: '',//banner文字误触系统屏蔽（android/ios）返回值为ios,则所有ios系统用户体验正常版本游戏，无任何误触
    }
    loadZSConfig() {
        // zsSdk.loadCfg((result) => {
        //     this.zsConfig = result;
        //     console.log(' result === ', result)
        //     // this.zsConfig.zs_advert_change = 0;
        //     // this.zsConfig.zs_switch = 1;
        //     // this.zsConfig.zs_auto_jump_switch2 = 1;
        //     // this.zsConfig.zs_banner_city = 1;
        //     // this.zsConfig.zs_click_award_num2 = 1;
        //     // this.zsConfig.zs_random_jump = 1;
        //     // this.zsConfig.zs_native_click_switch = 1;
        //     // this.zsConfig.zs_click_award_video = 1;
        //     // this.zsConfig.zs_banner_force_click = 0;
        //     // this.zsConfig.zs_auto_jump_switch = 1;
        //     // this.zsConfig.zs_video_delayed = 1;
        //     // this.zsConfig.zs_start_video_switch = 1;
        //     // this.zsConfig.zs_begin_click_switch = 1;
        //     // this.zsConfig.zs_banner_vertical_enable = 1;
        //     GlobalEvent.instance().emit(EventName.ZS_DATA)
        // }, (err) => {
        //     console.log(' err === ', err)
        //     setTimeout(() => {
        //         this.loadZSConfig()
        //     }, 3000);
        // });
    }
    loginZS() {
        // zsSdk.login((openid) => {
        //     this.loadZSConfig()
        //     console.log(' msg ', openid)
        //     this.openID = openid

        //     // zsSdk.init(this.openID)

        // }, (err) => {
        //     console.log('login error ', err)
        // })
    }

    getzs_advert_change() {
        if (!isNull(this.zsConfig.zs_advert_change)) {
            return this.zsConfig.zs_advert_change
        }
        return 0;
    }
    getzs_loading_video() {
        if (this.zsConfig.zs_loading_video) {
            return this.zsConfig.zs_loading_video == 1 && this.isSwtichOpen()
        }
        return false;
    }

    getzs_click_award_num2() {
        if (this.zsConfig.zs_click_award_num2) {
            return this.zsConfig.zs_click_award_num2 == 1 && this.isSwtichOpen()
        }
        return false;
    }

    getzs_banner_force_click() {
        if (this.zsConfig.zs_banner_force_click) {
            return this.zsConfig.zs_banner_force_click == 1
        }
        return false;
    }


    getClick_award_video() {
        if (this.zsConfig.zs_click_award_video) {
            return this.zsConfig.zs_click_award_video
        }
        return 0;
    }

    getzs_video_delayed() {
        if (this.zsConfig.zs_video_delayed) {
            return this.zsConfig.zs_video_delayed == 1
        }
        return false;
    }


    getAuto_jump_switch2() {
        if (this.zsConfig.zs_auto_jump_switch2) {
            return this.zsConfig.zs_auto_jump_switch2;
        }
        return 0;
    }
    getAuto_jump_switch() {
        if (this.zsConfig.zs_auto_jump_switch) {
            return this.zsConfig.zs_auto_jump_switch;
        }
        return 0;
    }
    getBannerDelayTime() {
        if (this.zsConfig.zs_banner_time != undefined) {
            return this.zsConfig.zs_banner_time
        }
        return 2;
        // return this.zsConfig.zs_banner_time;
    }



    getRandom_jump() {
        if (this.zsConfig.zs_random_jump) {
            return this.zsConfig.zs_random_jump;
        }
        return 0;
    }
    isVersion() {
        return compareVersion(this.verson, this.zsConfig.zs_version) == 1
    }

    isOpenNativeErrorClick() {
        return compareVersion(this.verson, this.zsConfig.zs_version) == 1 && this.zsConfig.zs_native_click_switch == 1
    }

    isSwtichOpen() {
        return this.zsConfig.zs_switch == 1
    }

    isSystemOpen() {
        if (this.zsConfig.zs_banner_system == "") {
            return true;
        }
        return this.getOS() != this.zsConfig.zs_banner_system
    }


    getOS() {
        return osMap[cc.sys.os]
    }



    isErrorClickOpen() {
        return this.isSwtichOpen() && this.isSystemOpen() && this.zsConfig.zs_banner_city == 1
    }


    isStartErrorClick() {
        return this.isErrorClickOpen() && this.zsConfig.zs_start_video_switch == 1;
    }

    isBannerErrorClick() {
        return this.isErrorClickOpen() && this.zsConfig.zs_banner_vertical_enable == 1;
    }


    loadAd() {
        console.log(' loadAd start')

        // zsSdk.loadAd((res) => {
        //     if (res && res.promotion) {
        //         this.adData.length = 0;
        //         for (let index = 0; index < res.promotion.length; index++) {
        //             const element = res.promotion[index];
        //             let game = new ZSGameItemModel(element);
        //             this.adData.push(game)
        //         }
        //         for (let index = 0; index < this.adData.length; index++) {
        //             const game = this.adData[index];
        //             this.setAdClickParam(game)
        //         }
        //         this.sortAdData()
        //     }

        //     // console.log(' loadAd data is ', res)

        // })
    }

    setAdClickParam(game: GameItemModel) {
        let gameName = game.getGameName()
        let list: GameItemModel[] = this.clickMap.get(gameName)
        if (isNull(list) || list.length <= 0) {
            list = []
            this.clickMap.set(gameName, list)
            for (let j = 0; j < this.adData.length; j++) {
                const element = this.adData[j];
                list.push(element)
            }
            // console.log('setAdClickParam length  ',list.length ,' gameName ',gameName)
        }

    }

    getAdData() {

        return this.adData
    }

    sortAdData() {
        if (this.adData.length > 0) {
            this.adData.sort((a: GameItemModel, b: GameItemModel) => {
                return b.getPriority() - a.getPriority()
            })
            GlobalEvent.instance().emit(EventName.SORT_AD_DATA)
        }

    }

    toOtherGame(model: GameItemModel, success?: Function, fail?: Function, complete?: Function) {
        if (this.openID) {
            let data = model.getData();
            // zsSdk.navigate2Mini(data, this.openID, success, fail, complete)
        }
    }

    navigate2Mini(model: GameItemModel, success?: Function, fail?: Function, complete?: Function) {
        if (this.openID) {
            let gameName = model.getGameName();
            let list = this.clickMap.get(gameName)
            if (list && list.length > 0) {
                let index = list.indexOf(model)
                // console.log('navigate2Mini length  ', list.length, ' gameName ', gameName, ' index ', index)
                if (index >= 0) {//存在这个游戏
                    let data = model.getData();
                    // console.log('navigate2Mini1111 appid ', data)
                    // zsSdk.navigate2Mini(data, this.openID, success, fail, complete)
                    list.splice(index, 1)
                } else {
                    //随机一个位置
                    let index = RandomHelper.random(0, list.length)
                    let game = list[index]
                    let data = game.getData();
                    // console.log('navigate2Mini2222 appid ', data)
                    // zsSdk.navigate2Mini(data, this.openID, success, fail, complete)
                    list.splice(index, 1)
                }

                if (list.length <= 0) {
                    this.setAdClickParam(model)
                }
            } else {
                this.setAdClickParam(model)
            }

        }
    }

    //是否弹出开始游戏后的砸蛋界面
    isOpenStartEgg() {
        let loginCount = User.instance().getLoginCount();
        return this.isErrorClickOpen() && this.zsConfig.zs_begin_click_switch == 1 &&
            loginCount >= this.zsConfig.zs_begin_click__since;
    }
}